#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
简化版数据导入脚本
"""

import sqlite3
import pandas as pd
import os
from pathlib import Path


def import_excel_to_sqlite():
    """将 Excel 文件导入到 SQLite 数据库"""

    # 设置路径
    project_root = Path(__file__).parent.parent
    data_source_dir = project_root / "Data Source"
    db_path = "Chinook.db"

    # 连接数据库
    conn = sqlite3.connect(db_path)

    try:

        # 导入 ModelCapaSsme.csv
        sample_file = data_source_dir / "基础表.xlsx"
        if sample_file.exists():
            print(f"正在导入 {sample_file}...")
            df_sample = pd.read_excel(sample_file)
            df_sample.to_sql("基础表", conn, if_exists="replace", index=False)
            print(f"基础表.xlsx 导入完成，共 {len(df_sample)} 行数据")
        else:
            print(f"文件不存在: {sample_file}")


        # 导入 Sample.xlsx
        # sample_file = data_source_dir / "Sample.xlsx"
        # if sample_file.exists():
        #     print(f"正在导入 {sample_file}...")
        #     df_sample = pd.read_excel(sample_file)
        #     df_sample.to_sql("sample_data", conn, if_exists="replace", index=False)
        #     print(f"Sample.xlsx 导入完成，共 {len(df_sample)} 行数据")
        # else:
        #     print(f"文件不存在: {sample_file}")

        # 导入基础表.xlsx
        # base_file = data_source_dir / "基础表.xlsx"
        # if base_file.exists():
        #     print(f"正在导入 {base_file}...")
        #     df_base = pd.read_excel(base_file)
        #     df_base.to_sql("base_data", conn, if_exists="replace", index=False)
        #     print(f"基础表.xlsx 导入完成，共 {len(df_base)} 行数据")
        # else:
        #     print(f"文件不存在: {base_file}")

        # 显示表信息
        cursor = conn.execute("SELECT name FROM sqlite_master WHERE type='table'")
        tables = cursor.fetchall()
        print(f"\n数据库中的表: {[table[0] for table in tables]}")

        print(f"\n数据已成功导入到 {db_path}")

    except Exception as e:
        print(f"导入过程中出现错误: {e}")

    finally:
        conn.close()


if __name__ == "__main__":
    import_excel_to_sqlite()
